package com.parking.mgr.useraccount.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parking.mgr.common.constant.CommonConstant;
import com.parking.mgr.common.exception.ServiceException;
import com.parking.mgr.common.exception.enums.CommonExceptionEnum;
import com.parking.mgr.common.factory.PageFactory;
import com.parking.mgr.useraccount.model.entity.ParkingRecordInfoEntity;
import com.parking.mgr.useraccount.model.entity.ParkingRecordReviewEntity;
import com.parking.mgr.useraccount.model.mapper.ParkingRecordInfoMapper;
import com.parking.mgr.useraccount.model.mapper.ParkingRecordReviewMapper;
import com.parking.mgr.useraccount.model.mapstruct.ParkingRecordInfoMapStruct;
import com.parking.mgr.useraccount.model.mapstruct.ParkingRecordReviewMapStruct;
import com.parking.mgr.useraccount.model.param.ParkingRecordInfoParam;
import com.parking.mgr.useraccount.model.param.ParkingRecordReviewParam;
import com.parking.mgr.useraccount.model.vo.ParkingRecordInfoVO;
import com.parking.mgr.useraccount.model.vo.ParkingRecordReviewVO;
import com.parking.mgr.useraccount.service.ParkingRecordInfoService;
import com.parking.mgr.useraccount.service.ParkingRecordReviewService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName ParkingRecordReviewService
 * @Description TODO
 * @author: puyunqing
 * @Date: 2025/3/9
 */
@Service
@Slf4j
public class ParkingRecordReviewServiceImpl extends ServiceImpl<ParkingRecordInfoMapper, ParkingRecordInfoEntity> implements ParkingRecordReviewService {

    @Resource
    private ParkingRecordInfoService parkingRecordInfoService;

    @Resource
    private ParkingRecordInfoMapStruct parkingRecordInfoMapStruct;

    @Override
    public void review(String id) {

        ParkingRecordInfoEntity parkingRecordInfoEntity = parkingRecordInfoService.getById(id);
        if (ObjectUtil.isNull(parkingRecordInfoEntity)) {
            throw new ServiceException("停车备案信息数据不存在");
        }

        parkingRecordInfoEntity.setFReviewStatus(CommonConstant.PENDING);

        this.save(parkingRecordInfoEntity);
    }

    @Override
    public void updateReview(ParkingRecordInfoParam parkingRecordInfoParam) {

        ParkingRecordInfoEntity parkingRecordInfoEntity = this.getById(parkingRecordInfoParam.getId());

        if (ObjectUtil.isNull(parkingRecordInfoEntity)) {
            throw new ServiceException("停车备案信息数据不存在");
        }

        parkingRecordInfoEntity.setFReviewStatus(parkingRecordInfoParam.getFReviewStatus());

        this.updateById(parkingRecordInfoEntity);
    }

    @Override
    public ParkingRecordInfoVO detail(String id) {

        ParkingRecordInfoEntity parkingRecordInfoEntity = this.getById(id);

        if (ObjectUtil.isNull(parkingRecordInfoEntity)) {
            throw new ServiceException(CommonExceptionEnum.DATA_NOT_EXIST);
        }

        return parkingRecordInfoMapStruct.entity2VO(parkingRecordInfoEntity);
    }

    @Override
    public IPage<ParkingRecordInfoVO> listReview(ParkingRecordInfoParam parkingRecordInfoParam, HttpServletRequest request) {

        LambdaQueryWrapper<ParkingRecordInfoEntity> queryWrapper = new LambdaQueryWrapper<ParkingRecordInfoEntity>()
                .ne(ParkingRecordInfoEntity::getFReviewStatus, CommonConstant.FILLING);
        if (ObjectUtil.isNotNull(parkingRecordInfoParam)) {
            if (StrUtil.isNotBlank(parkingRecordInfoParam.getFParkingName())) {
                queryWrapper.eq(ParkingRecordInfoEntity::getFParkingName, parkingRecordInfoParam.getFParkingName());
            }
            if (StrUtil.isNotBlank(parkingRecordInfoParam.getFReviewStatus())) {
                queryWrapper.eq(ParkingRecordInfoEntity::getFReviewStatus, parkingRecordInfoParam.getFReviewStatus());
            }
        }

        Page<ParkingRecordInfoEntity> page = PageFactory.defaultPage(parkingRecordInfoParam.getPageSize(), parkingRecordInfoParam.getCurrPage());

        return parkingRecordInfoMapStruct.entity2VO(this.page(page, queryWrapper));
    }

    @Override
    public Long countPending() {

        LambdaQueryWrapper<ParkingRecordInfoEntity> queryWrapper = new LambdaQueryWrapper<ParkingRecordInfoEntity>()
                .eq(ParkingRecordInfoEntity::getFReviewStatus, CommonConstant.PENDING);

        return this.count(queryWrapper);
    }

    @Override
    public Long countPass() {

        LambdaQueryWrapper<ParkingRecordInfoEntity> queryWrapper = new LambdaQueryWrapper<ParkingRecordInfoEntity>()
                .eq(ParkingRecordInfoEntity::getFReviewStatus, CommonConstant.PASS);

        return this.count(queryWrapper);
    }

    @Override
    public Long countRejected() {

        LambdaQueryWrapper<ParkingRecordInfoEntity> queryWrapper = new LambdaQueryWrapper<ParkingRecordInfoEntity>()
                .eq(ParkingRecordInfoEntity::getFReviewStatus, CommonConstant.REJECT);

        return this.count(queryWrapper);
    }
}
